package com.geekbang;

import com.geekbang.person.Customer;
import com.geekbang.supermarket.LittleSuperMarket;
import com.geekbang.supermarket.Merchandise;

import javax.swing.plaf.IconUIResource;
import java.util.Arrays;
import java.util.Scanner;

public class RunLittleSuperMarketAppMain {
    public static void main(String[] args) {
        LittleSuperMarket littleSuperMarket = new LittleSuperMarket();
        littleSuperMarket.address = "hangzhou";
        littleSuperMarket.superMarketName = "warm";
        littleSuperMarket.parkingCount = 200;
        littleSuperMarket.merchandises = new Merchandise[200];
        littleSuperMarket.merchandiseSold = new int[littleSuperMarket.merchandises.length];

        Merchandise[] all = littleSuperMarket.merchandises;
        for (int i = 0; i < all.length; i ++) {
            Merchandise m = new Merchandise();
            m.count = 200;
            m.id = "ID" + i;
            m.name = "product" + i;
            m.purchasePrice = Math.random() * 200;
            m.soldPrice = (1 + Math.random()) * 200;
            all[i] = m;
        }

        boolean open = true;
        Scanner scanner = new Scanner(System.in);

        while (open) {
            System.out.println(littleSuperMarket.superMarketName);

            Customer customer = new Customer();
            customer.name = "No." + (int)(Math.random() * 1000);
            customer.money = (1 + Math.random()) * 1000;
            customer.isDrivingCar = Math.random() > 0.5;

            if (customer.isDrivingCar) {
                if (littleSuperMarket.parkingCount > 0) {
                    System.out.println("welcome" + customer.name + "your parking lot number is " + littleSuperMarket.parkingCount);
                    littleSuperMarket.parkingCount --;
                } else {
                    System.out.println("parking lot is full, please welcome to next time");
                    continue;
                }
            } else {
                System.out.println("welcome");
            }

            double totalCost = 0;
            while (true) {
                System.out.println("please enter goods number 1-" + all.length);
                int index = scanner.nextInt();

                if (index < 0) {
                    break;
                }

                Merchandise m = all[index];
                System.out.println("the price of your goods is " + m.soldPrice + " how many goods do you want to buy?");
                int numberToBuy = scanner.nextInt();

                totalCost += numberToBuy * m.soldPrice;
                m.count -= numberToBuy;
                littleSuperMarket.merchandiseSold[index] += numberToBuy;
                break;
            }

            customer.money -= totalCost;

            if (customer.isDrivingCar) {
                littleSuperMarket.parkingCount ++;
            }

            littleSuperMarket.incomingSum = totalCost;

            System.out.println("do you want to open ?");
            open = scanner.nextBoolean();
        }
    }
}
